<?php
class IndexController extends TIGT_Controller_Action{
    protected $_arrParam;
    protected $_user;
    
    public function init(){
        Zend_Loader::loadClass('InputModel');
        $this->_arrParam = $this->_request->getParams();
        //      lay dia chi host
        $pageURL = 'http';
        if (!empty($_SERVER['HTTPS'])) {
            if ($_SERVER['HTTPS'] == 'on') {
                $pageURL .= "s";
            }
        }
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"];
        }
        $this->_host = $this->view->host = $pageURL;

        //      Bo di cac truong ko can thiet
        unset($this->_arrParam['module']);
        unset($this->_arrParam['controller']);
        unset($this->_arrParam['action']);

        //      Them vao view cac chung
        $this->view->controller = $this->_request->getControllerName();
        $this->view->action = $this->_request->getActionName();
        $this->view->module = $this->_request->getModuleName();
        //      Loc cac bien dau vao
        foreach ($this->_arrParam as &$p) {
            $p = InputModel::standardInputString($p);
        }
        //    	Truyá»�n tham sá»‘ ra view
        $this->view->arrParam = $this->_arrParam;
    }
    
//      Load cac file css, js, va khoi tao cac bien can thiet
    public function preDisPatch(){
//    	Chọn layout mặc định
    	
		$template_path = TEMPLATE_PATH . "/default/";
		$this->loadTemplate($template_path,'template.ini','template');
    }
    
    public function indexAction(){    
        
    }
    
    public function signUpAction(){
        // load file css va js can thiet        
        $this->view->headLink()->appendStylesheet("/css/default/form.css");
        $this->view->headScript()->appendFile("/js/default/validation.js");
        
        // Load model 
        Zend_Loader::loadClass("UserModel");
        
        // Xy ly du lieu nhan duoc
        try{
            
            //Tao captcha
            $captcha = new Zend_Captcha_Image();
            $captcha->setTimeout('300')                   
                      ->setWordLen('3')
                      ->setHeight('50')       
                      ->setWidth('200')     
                      ->setDotNoiseLevel(4)
                      ->setLineNoiseLevel(4)                     
                      ->setImgDir(PUBLIC_PATH . '/images/captcha')
                      ->setImgUrl('/images/captcha')
                      ->setFont(PUBLIC_PATH . '/images/captcha/times.ttf');           
             $captcha->generate();
             $this->view->captcha = $captcha->render($this->view); 
             $this->view->captchaID = $captcha->getId();
             // Dua chuoi Captcha vao session
             $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captcha->getId());
             $captchaSession->word = $captcha->getWord();
                
            $params = $this->_arrParam;
            if($this->_request->isPost()){                
                if(!empty($params['FullName']) && !empty($params['Gender']) && !empty($params["PhoneNumber"])
                && !empty($params['Address']) && !empty($params['FaceAccount']) && !empty($params["Introduce"])
                && !empty($params['BirthMonth']) && !empty($params['BirthDay']) && !empty($params["BirthYear"])
                && !empty($params['UserName']) && !empty($params['pass1']) && !empty($params["pass2"]) && !empty($params['captcha'])){
                    
                    $captchaID = $params['captcha_id'];
                    $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaID);
                    $captchaIterator = $captchaSession->getIterator();
                    $captchaWord = $captchaIterator['word'];
                    // Kiem tra captcha nhap vao
                    if($params['captcha'] == $captchaWord){
                        // kiem tra mat khau
                        if($params['pass1'] == $params['pass2']){
                            $user_model = new UserModel();
                            // Kiem tra username trung lap
                            if(!$user_model->getAccountByUserName($params['UserName'], 0)){   
                                // Insert Account
                                $data = array(
                                            "UserName"=>$params['UserName'],
                                            "Acc_PhoneNumber"=>$params['PhoneNumber'],
                                            "Password"=>md5($params['pass1']),
                                            "create_time"=>date("Y-m-d H:i:s"));
                                $account_id = $user_model->insertAccount($data);
                                
                                // Insert User info
                                $data = array("FullName"=>$params['FullName'],
                                            "Gender"=>$params['Gender'],
                                            "PhoneNumber"=>$params['PhoneNumber'],
                                            "Birthday"=>$params['BirthYear']."/".$params['BirthMonth']."/".$params['BirthDay'],
                                            "Address"=>$params['Address'],
                                            "FaceAccount"=>$params['FaceAccount'],
                                            "Introduce"=>$params['Introduce'],
                                            "idNormalUser"=>$account_id);                                            
                                $normal_user_id = $user_model->insert($data);
                                $this->view->success = "Đăng kí thành công";
                            }else{
                                $this->view->warning = "Account đã tồn tại";
                            }
                        }else{
                            $this->view->warning = "Mật khẩu nhập lại không khớp";
                        }
                    }else{
                        $this->view->warning = "Captcha nhập vào không đúng";
                    }
                }else{
                    $this->view->warning = "Thiếu dữ liệu nhập vào";
                }
            }
        }catch(Exception $e){
            echo $e->getMessage();die;
        }
    }
    public function testAction(){
        try{
        Zend_Loader::loadClass("SmsApiModel");
        //SmsApiModel::sendSMS("01682580630", "abc123");
        SmsApiModel::sendSMS("01676386786", "abc123");
        die("1");
        }catch(Exception $e){
            echo $e->getMessage();die;
        }   
    }
    public function authUserAction(){
        $params=$this->_arrParam;
        if ($_SERVER['REMOTE_ADDR'] <> "112.78.7.18"){
        	echo $_SERVER['REMOTE_ADDR']."can not access";
        	exit();
        }
        
        
        $cuphap=strtoupper($_REQUEST['message']);
        $phone= $_REQUEST['phone'];
        $service=$_REQUEST['service'];
        $tmp=explode(" ",$cuphap);
        if($tmp[0]=='FIBO')
        {
        	//set cu phap
        	if(@$tmp[1]=='XACTHUC')
        	{
        	    //$tmp[2] max xac thuc cua khach hang nhan tin
        		if(@$tmp[2]!=NULL)
        		{
        				echo '
				<ClientResponse>
					<Message>
						<PhoneNumber>'.$phone.'</PhoneNumber>
						<Message>Xac thuc thanh cong</Message>
						<SMSID> -1</SMSID>
						<ServiceNo>'.$service.'</ServiceNo>
					</Message>
				</ClientResponse>';
        			}
        			else
        			{
        				echo '
				<ClientResponse>
					<Message>
						<PhoneNumber>'.$phone.'</PhoneNumber>
						<Message>Ma so xac thuc khong dung</Message>
						<SMSID> -1</SMSID>
						<ServiceNo>'.$service.'</ServiceNo>
					</Message>
				</ClientResponse>';
        
        			}
        		}
        
        }
        die("Xac thuc thanh cong");
    }
    public function googleMapAction(){
        
    }
}